package com.langsin.blog.service.impl;

import com.langsin.blog.dao.ArticleDao;
import com.langsin.blog.dao.UserDao;
import com.langsin.blog.dao.impl.ArticleDaoImp;
import com.langsin.blog.dao.impl.UserDaoImp;
import com.langsin.blog.domain.Article;
import com.langsin.blog.domain.ArticleContent;
import com.langsin.blog.domain.User;
import com.langsin.blog.service.ArticleService;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ArticleServiceImp implements ArticleService{

    ArticleDao articleDao = new ArticleDaoImp();
    UserDao userDao = new UserDaoImp();
    boolean flag;

    @Test
    public void test(){
        this.articleSave("root1","两性与html","Html Css Js vue","MVVM");
    }

    public boolean articleSave(String account, String title, String brief,String content) {
            Article article = new Article();
            ArticleContent articleContent = new ArticleContent();
            articleContent.setText(content);
            article.setArticleContent(articleContent);
            article.setTitle(title);
            article.setArticleBrief(brief);
            User user = userDao.userSelectByAccount(account);
            article.setUser(user);
            article.setCreateTime(new Date());
            flag = articleDao.articleAdd(article);
            return flag;
    }

    public boolean articleModify(String id, String prop, String value) {
        flag = articleDao.articleModify(Integer.parseInt(id),prop,value);
        return flag;
    }

    public boolean articleDel(String id) {
        flag = articleDao.articleDel(Integer.parseInt(id));
        return flag;
    }

    public Article articleGet(String id) {
        Article article = articleDao.articleGet(id);
        return article;
    }

    @Test
    public void testListGet(){
        this.articleListGet("root1");
    }

    public List<Article> articleListGet(String account) {
        User user = userDao.userSelectByAccount(account);
        List<Article> artList = articleDao.articleListGet(user.getId());
        List<Article> articleList = new ArrayList<Article>();
        Article article = null;
        for (Article art : artList){
            article = new Article();
            article.setArticleBrief(art.getArticleBrief());
            article.setCreateTime(art.getCreateTime());
            article.setTitle(art.getTitle());
            article.setArticleContent(art.getArticleContent());
            article.setId(art.getId());
            articleList.add(article);
        }
        return articleList;
    }
}
